<template>
  <view>
    <view class="search-bg">
      <view class="example pad30">
        <uni-search-bar
          radius="100"
          placeholder="搜索药品名称"
          bgColor="#FFFFFF"
          v-model="value"
          @confirm="search"
          @cancel="cancelClick"
        />
      </view>
    </view>
    <view v-if="listData.length > 0">
      <view class="durgList" v-for="(item, index) in listData" :key="index">
        <view class="durgImg">
          <image :src="item.drugImg"></image>
        </view>
        <view class="durgInfo">
          <view class="durgName">{{ item.drugName }}</view>
          <view class="headline gray_font">{{ item.specification }}</view>
          <view class="price">
            <view class="priceMoney">
              <text>￥{{ item.drugPrice }}</text>
            </view>
            <view
              @click="addPrescription(item)"
              class="drugs_adjustment"
              size="mini"
            >
              加入处方笺
            </view>
          </view>
        </view>
      </view>
    </view>

    <view class="noInfo_Img" v-else-if="showNoInfo">
      <image
        src="../../../pagesSub/static/images/icon_lack_message@2x.png"
      ></image>
    </view>
    <!-- 滑动提示信息 -->
    <uni-load-more
      :status="statusLoad"
      :content-text="contentText"
      v-if="showLoadMore"
    />
  </view>
</template>

<script>
import { drugList,handleHistoryDrug } from '@/api/prescribing.js'
import store from '@/store'
export default {
  data() {
    return {
      listData: [],
      showLoadMore: false,
      showNoInfo: false,
      statusLoad: 'more',
      contentText: {
        contentdown: '查看更多',
        contentrefresh: '加载中...',
        contentnomore: '没有更多',
      },
      total: 0, //总条数
      nowCount: 0, //当前累计显示条数
      // 查询参数
      queryParams: {
        pageNum: 1, //第几页
        pageSize: 10, //每页显示条数
        searchName: '',
      },
      scriptionList: [],
	  doctorId:"",
    }
  },
  onLoad: function (option) {
	  if(option.doctorId){
		  this.doctorId=option.doctorId;
	  }
    let item = JSON.parse(decodeURIComponent(option.items))
    this.scriptionList = item
    // console.log("&&&&从上个页面带过来的数据",JSON.stringify(this.scriptionList))
    this.showLoadMore = false
    this.druginfo()
  },
  //滑动到底部触发
  onReachBottom() {
    if (this.nowCount > 0 && this.nowCount >= this.total) {
      this.statusLoad = 'noMore'
    } else {
      this.showLoadMore = true
      this.statusLoad = 'loading'
      this.druginfo()
    }
  },
  methods: {
    cancelClick() {
      this.nowCount = 0
      this.total = 0
      this.queryParams.pageNum = 1
      this.queryParams.searchName = ''
      this.druginfo()
    },
    druginfo() {
      let _this = this
      let newData
      let listData = _this.listData
      let nowCount = _this.nowCount
      let pageNum = _this.queryParams.pageNum
      if (_this.nowCount > 0 && _this.nowCount >= _this.total) return

      uni.showLoading({
        title: '加载中',
      })
      drugList(_this.doctorId, _this.queryParams)
        .then((res) => {
          let listNew = []

          if (_this.queryParams.pageNum != 1) {
            newData = listData.concat(res.rows)
          } else {
            newData = res.rows
          }
          _this.$set(_this, 'listData', newData) //使用$set可以避免this直接赋值，在组件上【有时】不及时生效的问题（偶尔会出现）
          //设置分页
          _this.total = res.total
          _this.queryParams.pageNum = pageNum + 1
          _this.nowCount = nowCount + res.rows.length
          if (pageNum == 1 && res.rows.length <= 0) {
            _this.showNoInfo = true
          }

          uni.hideLoading()
        })
        .catch((res) => {
          uni.hideLoading()
          console.log(res)
        })
    },
    addPrescription(item) {
		for (let i of this.scriptionList) {
					if (i.drugId == item.id) {
					  uni.showToast({
						title: '已加入此药品',
						icon: 'none',
						duration: 3000,
					  })
					  return
					}
		}
		let info=item;
		info.storeId=store.state.app.storeId; //药店id
		  handleHistoryDrug(info).then(res => {
		  	// console.log("*******res药品",JSON.stringify(res))
		  	uni.$emit('handClick', {
		  	  PrescriptionList: res.data,
		  	})
		  	uni.navigateBack()
		  	uni.hideLoading();
		  	
		  }).catch(res => {
		  	uni.hideLoading();
		  	uni.showToast({
		  		title:res,
		  		icon:'none',
		  		duration:3000
		  	})
		  	this.extraction=false;
		  	console.log('test错误='+res);
		  });
    },
    search(res) {
      let _this = this
      uni.showToast({
        title: '搜索：' + res.value,
        icon: 'none',
      })
      _this.queryParams.searchName = res.value
      _this.nowCount = 0
      _this.total = 0
      _this.queryParams.pageNum = 1
      this.druginfo()
    },
    cancel(res) {
      uni.showToast({
        title: '点击取消，输入值为：' + res.value,
        icon: 'none',
      })
    },
  },
}
</script>

<style scoped>
/deep/.uni-searchbar__text-placeholder.data-v-180dbe05 {
  font-size: 29rpx !important;
}
/deep/.uni-icons.data-v-a2e81f6e {
  font-size: 35rpx !important;
}
/deep/.uni-searchbar__cancel.data-v-180dbe05 {
  font-size: 29rpx !important;
}
/deep/.uni-searchbar__box-search-input.data-v-180dbe05 {
  font-size: 29rpx !important;
}
.drugs_adjustment {
  border-top-right-radius: 40rpx;
  border-bottom-right-radius: 40rpx;
  border-top-left-radius: 40rpx;
  border-bottom-left-radius: 40rpx;
  border: 1rpx solid #f3faff;
  background: -webkit-linear-gradient(#77e2a4, #4ccdbc);
  color: #ffffff;
  height: 55rpx;
  line-height: 55rpx;
  font-size: 26rpx;
  padding: 0 25rpx;
}
.durgList {
  display: flex;
  border-bottom: 1px solid #f7f7f7;
  padding: 10rpx 30rpx;
}
.durgImg {
  width: 200rpx;
  height: 100rpx;
  margin-top: 20rpx;
}
.durgImg image {
  width: 100%;
  height: 100%;
}
.durgInfo {
  width: 100%;
  margin-left: 30rpx;
}
.price {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.price > .priceMoney {
  color: #e26d5f;
  margin-top: 10rpx;
}
.durgName {
  font-size: 30rpx;
  margin-top: 10rpx;
}
.headline {
  font-size: 26rpx;
  margin-top: 5rpx;
}
</style>
